﻿using Dos.ORM;
using System.Data;
using orderCreater.Model;
using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;

namespace orderCreater
{
    public class WDH_CreaterModel
    {
        //MySql 数据库操作对象
        public DbSession db_w;
        public WDH_CreaterModel() 
        {
            db_w = new DbSession(DatabaseType.MySql, "Data Source=rm-bp1niu13oy19th27o.mysql.rds.aliyuncs.com;Database=webwdh;User Id=webwdh;Password=webwdh555%^^%%^*&&;Convert Zero Datetime=True;Allow Zero Datetime=True;");
        }

        /// <summary>
        /// 添加订单
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public string AddOrder(Db_order order)
        {
            string reVal = "0";
            db_w.Insert<Db_order>(order);
            reVal = db_w.Max<Db_order>(Db_order._.orderId, Db_order._.orderId != null).ToString();
            return reVal;
        }
        /// <summary>
        /// 添加订单
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public int AddOrderItem(Db_order_item order_item)
        {
            return db_w.Insert<Db_order_item>(order_item);
        }
        public Db_goods SelectGoodsByIds(string ids)
        {
            string[] sArray = Regex.Split(ids, ",", RegexOptions.IgnoreCase);
            if (sArray.Length > 0)
            {
                string mid = sArray[(new Random()).Next(0, 99999) % sArray.Length];
                List<Db_goods> list = db_w.From<Db_goods>().Where(Db_goods._.goodsId == mid).ToList<Db_goods>();
                if (list.Count > 0) return list[0];
                else return null;
            }
            else return null;
        }
        public Db_member SelectBuyerById(string id)
        {
            List<Db_member> list = db_w.From<Db_member>().Where(Db_member._.memberId == id).ToList<Db_member>();
            if (list.Count > 0) return list[0];
            else return null;
        }
        public decimal SelectTotalPrice(string short_date)
        {
            DateTime b_time = Convert.ToDateTime(short_date + " 00:00:00");
            DateTime e_time = Convert.ToDateTime(short_date + " 23:59:59");   
            string sum = db_w.Sum<Db_order>(Db_order._.totalPrice, Db_order._.addTime >= b_time && Db_order._.addTime <= e_time).ToString();
            decimal reVal = 0;
            try { reVal = Convert.ToDecimal(sum); }
            catch { reVal = 0; }
            return reVal;
        }
        public decimal SelectTotalPay(string short_date)
        {
            DateTime b_time = Convert.ToDateTime(short_date + " 00:00:00");
            DateTime e_time = Convert.ToDateTime(short_date + " 23:59:59");
            string sum = db_w.Sum<Db_order>(Db_order._.totalPrice, Db_order._.addTime >= b_time && Db_order._.addTime <= e_time && Db_order._.payStatusId==2).ToString();
            decimal reVal = 0;
            try { reVal = Convert.ToDecimal(sum); }
            catch { reVal = 0; }
            return reVal;
        }
    }
    
}
